import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, PackingList, PackingDetail} from './DTO';
const baseUrl = '/parts/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '包装箱单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const applyDesc = formatMessage({
    id: 'api.message.applyDesc',
    defaultMessage: '申请'
});

const rejectDesc = formatMessage({
    id: 'api.message.rejectDesc',
    defaultMessage: '驳回'
});


const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '初始化'
});

const packingDesc = formatMessage({
    id: 'api.message.packingDesc',
    defaultMessage: '查询备件装箱单'
});
// 初始化
export const initQuery = () => request(`${baseUrl}/ui/packingList/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));
// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/packingLists?${queryString}`, null, PackingList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id => request(`${baseUrl}/packingLists/${id}`, null, PackingDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, packingDesc));

// 申请
export const applyInfo = data => request(`${baseUrl}/packingLists/apply`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, applyDesc));

export const rejectInfo = data => request(`${baseUrl}/packingLists/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));

// 初始化
export const getInit = () => request(`${baseUrl}/ui/packingLists/apply/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, brandDesc));
// 查询包装箱单
export const getListApply = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/packingLists/apply?${queryString}`, null, PackingList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
